/**
  ******************************************************************************
  * @file    srv_encoder.c
  * @author  zwx
  * @version V1.0.0
  * @date    2019-09-21
  * @brief   
  ******************************************************************************
  */  

#include "srv_encoder.h"


/* Exported variables --------------------------------------------------------*/
/* Exported functions --------------------------------------------------------*/
/* Private typedef -----------------------------------------------------------*/
/* Private struct  -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
volatile int32_t Encoder[ENCODER_NUM] = {0};
volatile int32_t RobotDist = 0;

/* Private functions ---------------------------------------------------------*/

/**
  * @brief  
  * @param  None
  * @retval None
  */
void Srv_EncoderTim2Init(void)
{
    GPIO_InitTypeDef gpio;

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); 

	GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);  /* close JTAG */
	GPIO_PinRemapConfig(GPIO_PartialRemap1_TIM2, ENABLE); 
    
    gpio.GPIO_Pin   = GPIO_Pin_15;
    gpio.GPIO_Speed = GPIO_Speed_50MHz;
	gpio.GPIO_Mode  = GPIO_Mode_IPU;
	GPIO_Init(GPIOA, &gpio);
	
    gpio.GPIO_Pin   = GPIO_Pin_3;
    gpio.GPIO_Speed = GPIO_Speed_50MHz;
	gpio.GPIO_Mode  = GPIO_Mode_IPU;
	GPIO_Init(GPIOB, &gpio);	

    TIM_EncoderInterfaceConfig(TIM2, TIM_EncoderMode_TI12, TIM_ICPolarity_Falling, TIM_ICPolarity_Falling);			   
    TIM_Cmd(TIM2,ENABLE);
}

/**
  * @brief  
  * @param  None
  * @retval None
  */
void Srv_EncoderTim3Init(void)
{
    GPIO_InitTypeDef gpio;

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); 
	
	GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE); 
	
    gpio.GPIO_Pin   = GPIO_Pin_4;
    gpio.GPIO_Speed = GPIO_Speed_50MHz;
	gpio.GPIO_Mode  = GPIO_Mode_IPU;
	GPIO_Init(GPIOB, &gpio);
	
    gpio.GPIO_Pin   = GPIO_Pin_5;
    gpio.GPIO_Speed = GPIO_Speed_50MHz;
	gpio.GPIO_Mode  = GPIO_Mode_IPU;
	GPIO_Init(GPIOB, &gpio);	

    TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI12, TIM_ICPolarity_Falling, TIM_ICPolarity_Falling);			   
    TIM_Cmd(TIM3,ENABLE);

}


/**
  * @brief  
  * @param  None
  * @retval None
  */
void Srv_EncoderInit(void)
{
	Srv_EncoderTim2Init();
	Srv_EncoderTim3Init();
}

/**
  * @brief  
  * @param  None
  * @retval None
  */
void Srv_EncoderStart(void)
{
	TIM2->CNT = 0x7fff;
	TIM3->CNT = 0x7fff;
}


/**
  * @brief  
  * @param  None
  * @retval None
  */
void Srv_EncoderUpdate(void)
{
    int cnt = 0;
	
    cnt = (TIM2->CNT)-0x7fff;
    TIM2->CNT = 0x7fff;	
	Encoder[0] += cnt;

    cnt = (TIM3->CNT)-0x7fff;
    TIM3->CNT = 0x7fff;	
	Encoder[1] += cnt;

	RobotDist = (Encoder[0] + Encoder[1]) / 2;
}




/*****************************  End   ******************************************/



